package com.example.alibabanacosdiscoveryclient01.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;

/**
 * @author:guoq
 * @date:2024/6/27
 * @descripion:
 */
@RestController
@RequestMapping("/ai")
@Tag(name = "chatAI应用")
public class ChatAiController {
    private final ChatClient chatClient;
    @Autowired
    private ChatModel chatModel;

    public ChatAiController(ChatClient.Builder chatClient) {
//        this.chatClient=chatClient.build();
        this.chatClient = chatClient.defaultSystem("You are a friendly chat bot that answers question in the voice of a {voice}")
                .build();
    }

    @GetMapping("/chat")
    @Operation(summary = "简单聊天")
    public String chat(String message) {
        return this.chatClient.prompt()
                .user(message)
                .call()
                .content();
    }

    @GetMapping("/chat2")
    @Operation(summary = "chatResponse响应")
    public ChatResponse chat2(String message) {
        return this.chatClient.prompt()
                .user(message)
                .call()
                .chatResponse();
    }

    @GetMapping("/chat3")
    @Operation(summary = "流式响应")
    public Flux<ChatResponse> chat3(String message) {
        return this.chatClient.prompt()
                .user(message)
                .stream()
                .chatResponse();
//                .content();
    }

    @GetMapping("/chat4")
    @Operation(summary = "使用模板")
    public Map<String, String> chat4(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message, String voice) {
        return Map.of(
                "completion",
                chatClient.prompt()
                        .system(sp -> sp.param("voice", voice))
                        .user(message)
                        .call()
                        .content());
    }

    @GetMapping("/chat5")
    @Operation(summary = "使用ChatModel")
    public String chat5(String input) {
        ChatResponse response = chatModel.call(new Prompt(input));
//        return response.getResult().getOutput().getContent();
        return response.getResult().getOutput().getText();
    }

    @GetMapping("/promptTemplate")
    @Operation(summary = "使用promptTemplate")
    public AssistantMessage promptTemplate(String name,String adj) {
        String str = "给我讲一个有关{topic}的{adjective}的笑话";
        PromptTemplate promptTemplate = new PromptTemplate(str);
        //默认使用UserMessage生成提示
        Prompt prompt = promptTemplate.create(Map.of("topic", name, "adjective", adj));
        AssistantMessage content = chatClient.prompt(prompt)
                .call()
                .chatResponse()
                .getResult()
                .getOutput();
        return content;
    }
    @GetMapping("/promptTemplate2")
    @Operation(summary = "使用promptTemplate2")
    public AssistantMessage promptTemplate2(String desc,String name, String voice) {
        String userText =desc;
        Message userMessage = new UserMessage(userText);

        String systemText = """
         你是一个AI助手，帮助人们找到需要信息.
         你的名字是 {name}
         你应该回答用户提示的问题，并用 {voice} 声音回答它.
          """;
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "voice", voice));
        //设置用户提示和系统提示
        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
        AssistantMessage content = chatClient.prompt(prompt)
                .call()
                .chatResponse()
                .getResult()
                .getOutput();
        return content;

    }

}
